package com.itheima.leetcode.od.c.sort;

import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <h3>数组去重和排序</h3>
 */
public class ArrayDeduplicationAndSorting {
    public static void main(String[] args) {
        //处理输入
        Scanner in = new Scanner(System.in);
        //转为数组
        List<Integer> nums = Arrays.stream(in.nextLine().split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());

        // 注意使用LinkedHashMap，这样可以保留数字出现的顺序
        LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();
        for (int i = 0; i < nums.size(); i++) {
            map.put(nums.get(i), map.getOrDefault(nums.get(i), 0) + 1);
        }

        LinkedList<Map.Entry<Integer, Integer>> list = new LinkedList<>(map.entrySet());
        list.sort((a, b) -> b.getValue() - a.getValue());
        System.out.println(list.stream()
                .sorted((a, b) -> b.getValue() - a.getValue())
                .map(entry -> String.valueOf(entry.getKey()))
                .collect(Collectors.joining(",")));
    }
}